<?php
/**
 * gcFramework
 *
 * Development framework for PHP 5 or newer
 *
 * @package		gcFramework
 * @author		Adam Martin
 * @copyright	Copyright (c) 2006
 * @license		http://www.gcFramework.com/web/license.html
 * @link		http://www.gcFramework.com
 * @since		Version 1.0
 */
 
 //instantiate class
 $gcvalidate = new gcValidator;
 
class gcValidator{
	function email($email){
		// First, we check that there's one @ symbol, and that the lengths are right
  		if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
    		// Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
    		//displayError(ERROR_VALIDATION_EMAIL_NOTVALID);
    		return false;
  		}
  		// Split it into sections to make life easier
  		$email_array = explode("@", $email);
  		$local_array = explode(".", $email_array[0]);
  		for ($i = 0; $i < sizeof($local_array); $i++) {
     		if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
      			//displayError(ERROR_VALIDATION_EMAIL_NOTVALID);
      			return false;
    		}
  		}  
  		if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
    		$domain_array = explode(".", $email_array[1]);
    		if (sizeof($domain_array) < 2) {
        		//displayError(ERROR_VALIDATION_DOMAIN_NOTVALID);
        		return false;
    		}
    		for ($i = 0; $i < sizeof($domain_array); $i++) {
      			if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
        			//displayError(ERROR_VALIDATION_DOMAIN_NOTVALID);
        			return false;
      			}
    		}
  		}
		list($Username, $Domain) = split("@",$email);
		if(!fsockopen($Domain, 25, $errno, $errstr, 30)){
      		//displayError(ERROR_VALIDATION_EMAIL_NOTEXIST); 
      		return false;
   		}
   		return true;
	}
	function datecheck($date, $format){
		$date = str_replace("/", "-", $date);
 		$format = strtolower($format);
 		if (count($dateparts=explode('-', $date))!=3){
 			//displayError(ERROR_DATE_NOTCOMPLETE);
 			return false;	
 		}
 		$year = intval($dateparts[strpos($format, 'y')]);
  		$month = intval($dateparts[strpos($format, 'm')]);
  		$day = intval($dateparts[strpos($format, 'd')]);
  		if ($month<1 || $month>12){
  			//displayError(ERROR_DATE_MONTHINVALID);
  			return false;	
  		}
  		if ((($month==2) && ($day>28+(!($year%4))-(!($year%100))+(!($year%400)))) || ($day>30+(($month>7)^($month&1)))){
  			//displayError(ERROR_DATE_DAYINVALID);
  			return false;
 		}
 		return true;
	}
}
?>
